package de.koller.aframe.gl;

import de.koller.oceanFramework.gl.util.ParticleTexture;
import de.koller.oceanFramework.gl.util.TextureUtil;
import org.lwjgl.opengl.GL11;

abstract public class ATexture {
	
	private ParticleTexture texture = null;
	private int textureMipMap = -1;
	private int textureAnisotropic = -1;
	private int magFilter = GL11.GL_LINEAR;
	
	private ParticleTexture getTexture() {
		if( texture != null )
			return texture;
		
		TextureParameters p = new TextureParameters();
		this.setUp( p );
		magFilter = p.magFilter;
		
		ParticleTexture.MAG_FILTER = magFilter;
		ParticleTexture.MIN_FILTER = GL11.GL_LINEAR;
		texture = ParticleTexture.generate( p.size, p.size );
		
		p.builder.build( texture );
		texture.flush();
		
		return texture;
	}
	
	abstract protected void setUp( TextureParameters tparam );
	
	public void bind() {
		this.getTexture().bind();
	}
	
	public void bindMipMap() {
		if( textureMipMap == -1 ) {
			TextureUtil.MAG_FILTER = magFilter;
			textureMipMap = TextureUtil.makeMipMap( getTexture() );
		}
		
		TextureUtil.bind( textureMipMap );
	}
	
	public void bindAnisotrophic() {
		if( textureAnisotropic == -1 ) {
			TextureUtil.MAG_FILTER = magFilter;
			textureAnisotropic = TextureUtil.makeAnisotropic( getTexture() );
		}
		
		TextureUtil.bind( textureAnisotropic );
	}
	
	public static class TextureParameters {
		public int size = 32;
		public int magFilter = GL11.GL_LINEAR;
		public ITextureBuilder builder = null;
		
		private TextureParameters() {}
	}
	
	public static interface ITextureBuilder {
		public void build( ParticleTexture texture );
	}
}
